﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using IssueTracker.Business.Entity;
using IssueTracker.Business.BO;

namespace IssueTracker.Workflow.Activities
{
    [Designer(typeof(StateChangedDesigner), typeof(IDesigner))]
    public partial class StateChanged : System.Workflow.ComponentModel.Activity
	{
		public StateChanged()
		{
			InitializeComponent();
		}

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            UpdateIssueCurrentState((Issue)base.GetValue(ReportedIssueProperty));

            UpdateIssueTask((Issue) base.GetValue(ReportedIssueProperty));
            
            return ActivityExecutionStatus.Closed;
        }

        private void UpdateIssueCurrentState(Issue issue)
        {
            Issue newIssue = null;
            IssueBO issueBo = new IssueBO();

            newIssue = issueBo.GetIssue(issue.Id);

            if (newIssue != null)
            {
                if(this.Parent.Name != null)
                    newIssue.CurrentStateName = this.Parent.Parent.Description;
                else 
                    newIssue.CurrentStateName = this.QualifiedName;

                issueBo.UpdateIssue(newIssue);
            }
        }

        private void UpdateIssueTask(Issue issue)
        {
            IssueTask task = new IssueTask();
            IssueTaskBO taskBo = new IssueTaskBO();

            task.Id = System.Guid.NewGuid();
            task.IssueId = issue.Id;

            if (this.Parent.Name != null)
                task.StateName = this.Parent.Parent.Description;
            else
                task.StateName = this.QualifiedName;

            //TODO: How to get the ActivityId?
            //task.WfActivityId = this.ActivityId;
            //task.WfTaskId = this.TaskId;
            

            //TODO: How to get current login user id?
            task.CreatorId=System.Threading.Thread.CurrentPrincipal.Identity.Name;
            
            //todo: code a routing engine for this
            task.HolderId = "";

            taskBo.AddNew(task);
            
            
        }

        private void CreateIssueWfHistories()
        {

        }

        #region Properties

        public static DependencyProperty ReportedIssueProperty = DependencyProperty.Register("ReportedIssue", typeof(IssueTracker.Business.Entity.Issue), typeof(StateChanged));
        public static DependencyProperty StateNameProperty = DependencyProperty.Register("StateName", typeof(System.String), typeof(StateChanged));
        public static DependencyProperty routeToUserTypeProperty = DependencyProperty.Register("routeToUserType", typeof(RouteToUserType), typeof(StateChanged));
        public static DependencyProperty routeSettingProperty = DependencyProperty.Register("routeSetting", typeof(System.String), typeof(StateChanged));

        [Description("Reported Issue")]
        [Browsable(true)]
        [Category("Parameters")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Issue ReportedIssue
        {
            set { base.SetValue(StateChanged.ReportedIssueProperty, value); }
            get { return (Issue)base.GetValue(StateChanged.ReportedIssueProperty); }
        }

        [Description("State Name")]
        [Browsable(true)]
        [Category("Parameters")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string StateName
        {
            set { base.SetValue(StateChanged.StateNameProperty, value); }
            get { return (string)base.GetValue(StateChanged.StateNameProperty); }
        }

        [Description("Route To User Type")]
        [Browsable(true)]
        [Category("Routing")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public RouteToUserType RouteToUserType
        {
            set { base.SetValue(routeToUserTypeProperty, value); }
            get { return (RouteToUserType)base.GetValue(routeToUserTypeProperty); }
        }

        [Description("Route Setting")]
        [Browsable(true)]
        [Category("Routing")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string RouteSetting
        {
            set { base.SetValue(routeSettingProperty, value); }
            get { return (string)base.GetValue(routeSettingProperty); }
        }


        #endregion
    }

    public enum RouteToUserType
    {
        SingleUser,
        UserGroup
    }
}
